package com.vca.front.controller;


import com.vca.common.page.CommonPage;
import com.vca.common.request.PageParamRequest;
import com.vca.common.request.UserEditRequest;
import com.vca.common.response.*;
import com.vca.common.result.CommonResult;
import com.vca.service.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户 -- 用户中心
 */
@Slf4j
@RestController("FrontUserController")
@RequestMapping("api/front/user")
@Api(tags = "用户 -- 我的")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 修改个人资料
     */
    @ApiOperation(value = "修改个人资料")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public CommonResult<Boolean> personInfo(@RequestBody @Validated UserEditRequest request) {
        if (userService.editUser(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 个人中心-用户信息
     */
    @ApiOperation(value = "我的-用户信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<UserInfoResponse> getUserCenter(@RequestHeader(value="language",required = false)String language) {
        return CommonResult.success(userService.getUserCenter(language));
    }

    /**
     * 个人中心-会员码
     */
    @ApiOperation(value = "我的-会员码")
    @RequestMapping(value = "/qrcode", method = RequestMethod.GET)
    public CommonResult<Map<String, String>> MembershipCode() {
        return CommonResult.success(userService.getQrcode());
    }

    /**
     * 个人中心-我的预约项目
     */
    @ApiOperation(value = "我的-我的预约项目")
    @RequestMapping(value = "/appointment", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mainType", value = " 0=课程 2=讲座 3=展览", required = true)
    })
    public CommonResult<CommonPage<MyAppointmentItemResponse>> getAppointmentItem(@RequestHeader(value = "language",required = false) String language,
                                                                                    PageParamRequest pageParamRequest,
                                                                                  @RequestParam(value = "mainType") Integer mainType) {
        CommonPage<MyAppointmentItemResponse> restPage = CommonPage.restPage(userService.getAppointment(language,pageParamRequest, mainType));
        return CommonResult.success(restPage);
    }

    /**
     * 个人中心-礼品卡
     */
    @ApiOperation(value = "我的-礼品卡")
    @RequestMapping(value = "/card", method = RequestMethod.GET)
    public CommonResult<CommonPage<GiftCardResponse>> getGiftCard(
            @RequestHeader(value = "language",required = false) String language,
            @ApiParam(value = "预下单id")
            @RequestParam(value = "preOrderNo", required = false) String preOrderNo,
            @Validated PageParamRequest pageParamRequest) {
        CommonPage<GiftCardResponse> restPage = CommonPage.restPage(userService.getCard(language,preOrderNo, pageParamRequest));
        return CommonResult.success(restPage);
    }

    /**
     * 个人中心-礼品卡详情
     */
    @ApiOperation(value = "我的-礼品卡详情")
    @RequestMapping(value = "/card/info", method = RequestMethod.GET)
    public CommonResult<GiftCardInfoResponse> getGiftCardInfo(
            @RequestHeader(value = "language",required = false) String language,
            @RequestParam(value = "id")
            @ApiParam(value = "ID")
            Integer id) {
        return CommonResult.success(userService.getCardInfo(language,id));
    }

    /**
     * @Description:礼品卡领取页详情
     * @author:chenbing
     * @date 2023/6/27 9:26
     */
    @ApiOperation(value = "礼品卡领取页详情")
    @GetMapping(value = "/card/info/claim")
    public CommonResult<HashMap<String, Object>> cardInfoClaim(
            @RequestParam(value = "cardNo") String cardNo
    ) {
        return CommonResult.success(userService.cardInfoClaim(cardNo));
    }

    /**
     * @Description:获取相应的礼品卡
     * @author:chenbing
     * @date 2022/12/21 14:49
     */
    @ApiOperation(value = "获取相应的礼品卡")
    @GetMapping(value = "/get/card/list")
    public CommonResult<CommonPage<GiftCardResponse>> getCardList(
            @ApiParam(value = "课程/讲座/展览的主键ID")
            @RequestParam(value = "mainId") Long mainId,
            @ApiParam(value = "0=课程 2=讲座 3=展览")
            @RequestParam(value = "type") Integer type,
            @Validated PageParamRequest pageParamRequest
    ) {
        return CommonResult.success(CommonPage.restPage(userService.getCardList(mainId, type, pageParamRequest)));
    }

    /**
     * @Description:兑换礼品卡
     * @author:chenbing
     * @date 2023/9/26 10:23
     */
    @ApiOperation(value = "兑换礼品卡")
    @GetMapping(value = "exchangeCard")
    public CommonResult<String> exchangeCard(
            @RequestHeader(value = "language",required = false) String language,
            @ApiParam(value = "礼品卡序列号")
            @RequestParam(value = "cardKey") String cardKey
    ) {
        if (userService.exchangeCard(language,cardKey)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 个人中心-收藏
     */
    @ApiOperation(value = "我的-收藏")
    @RequestMapping(value = "/collection", method = RequestMethod.GET)
    public CommonResult<CommonPage<CollectionsResponse>> getCollection(@RequestHeader(value = "language",required = false) String language, PageParamRequest pageParamRequest) {
        CommonPage<CollectionsResponse> restPage = CommonPage.restPage(userService.getCollections(language,pageParamRequest));
        return CommonResult.success(restPage);
    }

    /**
     * @Description:礼品卡接受赠送
     * @author:chenbing
     * @date 2022/12/1 19:44
     */
    @ApiOperation(value = "礼品卡接受赠送")
    @PostMapping(value = "/getCard")
    public CommonResult<String> getCard(
            @ApiParam(value = "赠送人")
            @RequestParam(value = "sendUser") Integer sendUser,
            @ApiParam(value = "礼品卡orderNo")
            @RequestParam(value = "cardNo") String cardNo
    ) {
        if (userService.getCardToSend(sendUser, cardNo)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

}



